<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="cn.pzhu.jspdb.dao.UserDao" %>
<%@ page import="cn.pzhu.jspdb.bean.User" %>
<%@ page import="java.util.List" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="add.jsp">Add User</a>
	<table border="1">
		<tr>
			<td>ID</td>
			<td>LoginName</td>
			<td>Password</td>
			<td>Operate</td>
		</tr>
		<%
			String pStr=request.getParameter("p");
			String countStr=request.getParameter("count");
			int p=1;
			int count=5;
			if(pStr!=null){
				p=Integer.parseInt(pStr);
			}
			if(countStr!=null){
				count=Integer.parseInt(countStr);
			}
			int startIndex=(p-1)*count;
			UserDao dao=new UserDao();
			List<User> users=dao.listAll(startIndex,count);
			for(User u:users){ //foreach
		%>
		<tr>
			<td><%=u.getId() %></td>
			<td><%=u.getLoginName() %></td>
			<td><%=u.getPassword() %></td>
			<td>
				<a href="del.jsp?id=<%=u.getId()%>">Del</a>
				<a href="edit.jsp?id=<%=u.getId()%>">Edit</a>
			</td>
		</tr>
		<%
			}
		%>
	</table>
	<%
		long total=dao.countAll();
		int pageSize=(int)total/count+(total%count==0?0:1);
		for(int i=1;i<=pageSize;i++){
			if(p==i){
				out.print(i);
			}else{
	%>
	
	<a href="page.jsp?p=<%=i%>&count=<%=count %>"><%=i%></a>
	<%	}
	} %>
	
	<%
		if(p>1&&p<=pageSize){
	%>
		<a href="page.jsp?p=<%=p-1%>&count=<%=count %>">前一页</a>
	<%} %>
	<%
		if(p>=1&&p<pageSize){
	%>
		<a href="page.jsp?p=<%=p+1%>&count=<%=count %>">下一页</a>
	<%} %>
	<br/>总记录数:<%=total%> 总页数:<%=pageSize %>
</body>
</html>